﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.DataAccessLayer;
using CuaHangMayTinh.BusinessEntities;
namespace CuaHangMayTinh.PresentationLayer
{
    public partial class FormInfo : Form
    {
        public FormInfo()
        {
            InitializeComponent();
        }
        public FormInfo(bool[] so)
            : this()
        {
            if (so[0])
            {
                EnableControl(true);
            }
        }

        public void EnableControl(bool i)
        {
            foreach (Control item in this.Controls)
            {
                item.Enabled = i;
            }
        }

        /// <summary>
        /// Sự kiện load form Info
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormInfo_Load(object sender, EventArgs e)
        {
            InfoEntity entity =  InfoManager.ConvertToList(InfoManager.GetThongTin())[0];
            SetEntity(entity);
        }
        /// <summary>
        /// Lấy thông tin entity và các textbox
        /// </summary>
        /// <param name="entity"></param>
        void SetEntity(InfoEntity entity)
        {
            txt_tendonvi.Text = entity.TenDonVi;
            txt_diachi.Text = entity.DiaChi;
            txt_dienthoai.Text = entity.DienThoai;
            txt_fax.Text = entity.Fax;
            txt_masothue.Text = entity.MaSoThue;
            txt_email.Text = entity.Email;
            txt_website.Text = entity.Website;
            txt_mota.Text = entity.MoTa;
        }
        /// <summary>
        /// Lấy thông tin từ form trả về entity
        /// </summary>
        /// <returns></returns>
        InfoEntity GetEntity()
        {
            InfoEntity entity = new InfoEntity();
            entity.TenDonVi = txt_tendonvi.Text;
            entity.DiaChi = txt_diachi.Text;
            entity.DienThoai = txt_dienthoai.Text;
            entity.Fax = txt_fax.Text;
            entity.MaSoThue = txt_masothue.Text;
            entity.Email = txt_email.Text;
            entity.Website = txt_website.Text;
            entity.MoTa= txt_mota.Text;

            return entity;
        }
        /// <summary>
        /// sự kiện đóng form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// sự kiện lưu lại thông tin vừa sửa.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_luu_Click(object sender, EventArgs e)
        {
            InfoEntity entity = GetEntity();
            if (InfoManager.UpDate(entity))
            {
                MessageBox.Show("Lưu Thành Công");
            }
            else
            {
                MessageBox.Show("Lưu Thất Bại");
                InfoEntity entityold = InfoManager.ConvertToList(InfoManager.GetThongTin())[0];
                SetEntity(entityold);
            }
        }
    }
}
